2020-2021 Sunseeker Telemetry and Lighting System
tlv_ex3_calibrateTempSensor.c
Go to the documentation of this file.
1 /* --COPYRIGHT--,BSD
2  * Copyright (c) 2017, Texas Instruments Incorporated
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Texas Instruments Incorporated nor the names of
17  * its contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  * --/COPYRIGHT--*/
32 //******************************************************************************
70 //******************************************************************************
71 
72 #include "driverlib.h"
73 
74 uint16_t temp;
75 volatile float DegF;
76 volatile float DegC;
77 float mref, nref;
78 uint8_t bADCCAL_bytes;
79 struct s_TLV_ADC_Cal_Data * pADCCAL;
80 
81 //******************************************************************************
82 //
83 // This function will initialize and calibrate ADC for temperature sensing.
84 // The embedded function call of Get_TLV_Info searches the TLV block of data
85 // (defined in the device header file) for the ADC calibration block, then
86 // returns the address of the first data descriptor to the pointer passed into
87 // it. The slope and offset of the corrected temperature curve are then
88 // calculated for temperature calibration purposes.
89 //
90 //******************************************************************************
91 void calibrateADC(void);
92 
93 void main (void)
94 {
95  //Stop Watchdog Timer
96  WDT_A_hold(WDT_A_BASE);
97 
98  calibrateADC();
99 
100  while (1)
101  {
102  //Enable/Start first sampling and conversion cycle
103  /*
104  * Base address of ADC12 Module
105  * Start the conversion into memory buffer 0
106  * Use the repeated sequence of channels
107  */
108  ADC12_A_startConversion(ADC12_A_BASE,
109  ADC12_A_MEMORY_0,
110  ADC12_A_SEQOFCHANNELS);
111 
112  //LPM0 with interrupts enabled
113  __bis_SR_register(LPM4_bits + GIE);
114  __no_operation();
115 
116  //Temperature in Celsius
117  DegC = (temp - nref) / mref;
118 
119  //Temperature in Fahrenheit
120  DegF = (9*DegC)/5 + 32;
121 
122  //SET BREAKPOINT HERE and watch IntDegC and IntDegF
123  __no_operation();
124  }
125 }
126 
127 void calibrateADC(void)
128 {
129  //Initialize the ADC12 Module
130  /*
131  * Base Address of ADC12 Module
132  * Use internal ADC12 bit as sample/hold signal to start conversion
133  * USE MODOSC 5MHZ Digital Oscillator as clock source
134  * Use default clock divider of 1
135  */
136  ADC12_A_init(ADC12_A_BASE,
137  ADC12_A_SAMPLEHOLDSOURCE_SC,
138  ADC12_A_CLOCKSOURCE_ADC12OSC,
139  ADC12_A_CLOCKDIVIDER_1);
140 
141  ADC12_A_enable(ADC12_A_BASE);
142 
143  /*
144  * Base Address of ADC12 Module
145  * For memory buffers 0-7 sample/hold for 256 clock cycles
146  * For memory buffers 8-15 sample/hold for 4 clock cycles (default)
147  * Disable Multiple Sampling
148  */
149  ADC12_A_setupSamplingTimer(ADC12_A_BASE,
150  ADC12_A_CYCLEHOLD_256_CYCLES,
151  ADC12_A_CYCLEHOLD_4_CYCLES,
152  ADC12_A_MULTIPLESAMPLESDISABLE);
153 
154  //Configure Memory Buffer
155  /*
156  * Base Addres of ADC12 Module
157  * Configure memory buffer 0
158  * Map temp sensor to memory buffer 0
159  * Vref+ = Vref+ (int)
160  * Vref- = AVss
161  * Memory buffer 0 is not the end of a sequence
162  */
163  ADC12_A_configureMemoryParam param = {0};
164  param.memoryBufferControlIndex = ADC12_A_MEMORY_0;
165  param.inputSourceSelect = ADC12_A_INPUT_TEMPSENSOR;
166  param.positiveRefVoltageSourceSelect = ADC12_A_VREFPOS_INT;
167  param.negativeRefVoltageSourceSelect = ADC12_A_VREFNEG_AVSS;
168  param.endOfSequence = ADC12_A_NOTENDOFSEQUENCE;
169  ADC12_A_configureMemory(ADC12_A_BASE ,&param);
170 
171 
172  //Enable memory buffer 0 interrupt
173  ADC12_A_clearInterrupt(ADC12_A_BASE,
174  ADC12IFG0);
175  ADC12_A_enableInterrupt(ADC12_A_BASE,
176  ADC12IE0);
177 
178  //Configure internal reference
179  //If ref generator busy, WAIT
180  while ( REF_ACTIVE == Ref_isRefGenBusy(REF_BASE) ) ;
181  //Select internal ref = 1.5V
182  Ref_setReferenceVoltage(REF_BASE,
183  REF_VREF1_5V);
184  //Internal Reference ON
185  Ref_enableReferenceVoltage(REF_BASE);
186 
187  //Delay (~75us) for Ref to settle
188  __delay_cycles(75);
189 
190  // TLV access Function Call
191  TLV_getInfo(TLV_TAG_ADCCAL,
192  0,
193  &bADCCAL_bytes,
194  (uint16_t **)&pADCCAL
195  );
196 
197  mref = ((float)
198  (pADCCAL->adc_ref15_85_temp-pADCCAL->adc_ref15_30_temp))/(85-30);
199  nref = pADCCAL->adc_ref15_85_temp-mref*85;
200 }
201 
202 #if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
203 #pragma vector=ADC12_VECTOR
204 __interrupt
205 #elif defined(__GNUC__)
206 __attribute__((interrupt(ADC12_VECTOR)))
207 #endif
208 void ADC12ISR (void)
209 {
210  switch (__even_in_range(ADC12IV,34)){
211  case 0: break; //Vector 0: No interrupt
212  case 2: break; //Vector 2: ADC overflow
213  case 4: break; //Vector 4: ADC timing overflow
214  case 6: //Vector 6: ADC12IFG0
215  //Move results, IFG is cleared
216  temp = ADC12_A_getResults(ADC12_A_BASE,
217  ADC12_A_MEMORY_0);
218 
219  //Exit active CPU
220  __bic_SR_register_on_exit(LPM4_bits);
221  break;
222  case 8: break; //Vector 8: ADC12IFG1
223  case 10: break; //Vector 10: ADC12IFG2
224  case 12: break; //Vector 12: ADC12IFG3
225  case 14: break; //Vector 14: ADC12IFG4
226  case 16: break; //Vector 16: ADC12IFG5
227  case 18: break; //Vector 18: ADC12IFG6
228  case 20: break; //Vector 20: ADC12IFG7
229  case 22: break; //Vector 22: ADC12IFG8
230  case 24: break; //Vector 24: ADC12IFG9
231  case 26: break; //Vector 26: ADC12IFG10
232  case 28: break; //Vector 28: ADC12IFG11
233  case 30: break; //Vector 30: ADC12IFG12
234  case 32: break; //Vector 32: ADC12IFG13
235  case 34: break; //Vector 34: ADC12IFG14
236  default: break;
237  }
238 }
239 
void calibrateADC(void)
volatile float DegF
uint16_t temp
void main(void)
uint8_t bADCCAL_bytes
struct s_TLV_ADC_Cal_Data * pADCCAL
void ADC12ISR(void)
volatile float DegC
MPU_initThreeSegmentsParam param
__no_operation()
__bic_SR_register_on_exit(LPM3_bits|GIE)
__delay_cycles(500000)